package com.maotouying.tmtapp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dueeeke.videocontroller.component.PrepareView;
import com.maotouying.tmtapp.R;
import com.maotouying.tmtapp.entity.MovietvCategory;
import com.maotouying.tmtapp.entity.VideoEntity;
import com.maotouying.tmtapp.listener.OnItemChildClickListener;
import com.maotouying.tmtapp.listener.OnItemClickListener;
import com.maotouying.tmtapp.view.CircleTransform;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * @author mingtai
 * @description 写一个适配器，用来展示数据
 * @time 2020/8/11 0:21
 */

public class MyCollectAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    //上下文
    private Context mContext;
    //传过来的对象
    private List<VideoEntity> datas;

    public void setDatas(List<VideoEntity> datas) {
        this.datas = datas; //不用构造器，可以不用反复创建对象
    }

    private OnItemChildClickListener mOnItemChildClickListener;

    private OnItemClickListener mOnItemClickListener;

    public MyCollectAdapter(Context context) {
        this.mContext = context;
    }

    public MyCollectAdapter(Context context, List<VideoEntity> datas) {
        this.mContext = context;
        this.datas = datas;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 返回每一项展示的效果(视频的)
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_mycollect_layout, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        //    绑定数据，给控件对象赋值。
        ViewHolder vh = (ViewHolder) holder;
        VideoEntity videoEntity = datas.get(position);

        vh.tvTitle.setText(videoEntity.getVtitle());
        vh.tvAuthor.setText(videoEntity.getAuthor());

        // 异步加载图片
        Picasso.with(mContext)
                .load(videoEntity.getHeadurl())
                .transform(new CircleTransform()) // 绘制圆形头像
                .into(vh.imgHeader);
        //mThumb 缩略图
        Picasso.with(mContext).load(videoEntity.getCoverurl()).into(vh.mThumb);
        vh.mPosition = position; // 赋值下标

    }

    @Override
    public int getItemCount() {
        // recycleView 列表里面有多少项
        if (datas != null && datas.size() > 0) {
            return datas.size();
        }
        return 0;
    }

    // 写一个类，获取view对象中的每一条数据
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private TextView tvTitle;
        private TextView tvAuthor;
        private ImageView imgHeader;

        // private ImageView imgCover; 在这里使用播放器自己的
        // 以下三个是播放器专用的
        public ImageView mThumb;
        public PrepareView mPrepareView;
        public FrameLayout mPlayerContainer;
        // 记录下标
        public int mPosition;

        public ViewHolder(@NonNull View view) {
            super(view);
            tvTitle = view.findViewById(R.id.title);
            tvAuthor = view.findViewById(R.id.author);
            imgHeader = view.findViewById(R.id.img_header);
            // imgCover = view.findViewById(R.id.img_cover);

            mPlayerContainer = view.findViewById(R.id.player_container);
            mPrepareView = view.findViewById(R.id.prepare_view);
            mThumb = mPrepareView.findViewById(R.id.thumb);

            // 在播放器布局周围设置两个点击事件
            if (mOnItemChildClickListener != null) {
                mPlayerContainer.setOnClickListener(this);
            }
            if (mOnItemClickListener != null) {
                view.setOnClickListener(this);
            }

            //通过tag将ViewHolder和itemView绑定
            view.setTag(this);
        }

        // 点击事件的方法
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.player_container) {
                if (mOnItemChildClickListener != null) {
                    mOnItemChildClickListener.onItemChildClick(mPosition);
                }
            } else {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(mPosition);
                }
            }
        }
    }

    // 设置点击监听
    public void setOnItemChildClickListener(OnItemChildClickListener onItemChildClickListener) {
        mOnItemChildClickListener = onItemChildClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }


}
